package com.erp.statistics.order.controller;

import com.erp.statistics.order.service.IErpOrderInfoStatisticsService;
import com.erp.statistics.order.service.IErpChildOrderStatisticsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.order.api.domain.ErpSellerOrder;
import com.ruoyi.statistics.api.domain.ErpChildOrderStatistics;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单信息统计
 *
 * @author erp
 * @date 2023-07-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/statistics/order")
public class ErpOrderInfoStatisticsController extends BaseController {

    @Resource
    private IErpChildOrderStatisticsService childOrderStatisticsService;
    @Resource
    private IErpOrderInfoStatisticsService orderInfoStatisticsService;

//    private final IErpOrderInfoStatisticsService iErpOrderInfoStatisticsService;
//    private final IErpChildOrderStatisticsService iErpChildOrderStatisticsService;
//

    /**
     * 根据订单号，查询订单统计信息
     * @param orderId
     * @return
     */
    @GetMapping("/getByOrderId/{orderId}")
    public R<Object> getByOrderId(@PathVariable String orderId) {
        return R.ok(orderInfoStatisticsService.queryByOrderId(orderId));
    }

    /**
     * 根据子订单号，查询子订单统计信息
     * @param childOrderId
     * @return
     */
    @GetMapping("/getByChildOrderId/{childOrderId}")
    public R<Object> getByChildOrderId(@PathVariable String childOrderId) {
        return R.ok(childOrderStatisticsService.queryByChildOrderId(childOrderId));
    }

    /**
     * 保存订单和子订单的商品金额信息(利润等)
     * @param sellerOrder
     * @return
     */
    @PostMapping("/saveOrderProfitInfo")
    public R<Object> saveOrderProfitInfo(@RequestBody ErpSellerOrder sellerOrder) {
        int i = orderInfoStatisticsService.saveOrderProfitInfo(sellerOrder, sellerOrder.getSellerChildOrderList());
        if(i > 0){
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 根据条件查询数据列表
     * @param storeId
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getChildOrderStatisticsList/{storeId}/{startTime}/{endTime}")
    public R<Object> getChildOrderStatisticsList(@PathVariable("storeId") String storeId,
                                                 @PathVariable("startTime") String startTime,
                                                 @PathVariable("endTime") String endTime){
        List<ErpChildOrderStatistics> statisticsList
                = childOrderStatisticsService.selectChildOrderStatisticsList(Long.parseLong(storeId),
                DateUtils.parseDate(startTime), DateUtils.parseDate(endTime));
        if (statisticsList != null && statisticsList.size() > 0){
            return R.ok(statisticsList);
        }
        return R.fail();
    }
//
//    /**
//     * 以SkuCode为主查询
//     */
//    @GetMapping("/getStaticsByCode")
//    public TableDataInfo<ErpChildOrderStatisticsVo> getStaticsByCode(PageQuery pageQuery, ErpChildOrderStatisticsBo bo) {
//
//        return iErpChildOrderStatisticsService.getStaticsByCode(bo,pageQuery);
//    }
//
//    /**
//     * 查询订单信息统计列表
//     */
////    @SaCheckPermission("system:orderInfoStatistics:list")
//    @GetMapping("/list")
//    public TableDataInfo<ErpOrderInfoStatisticsVo> list(ErpOrderInfoStatisticsBo bo, PageQuery pageQuery) {
//        return iErpOrderInfoStatisticsService.queryPageList(bo, pageQuery);
//    }
//
//    /**
//     * 导出订单信息统计列表
//     */
//    @SaCheckPermission("system:orderInfoStatistics:export")
//    @Log(title = "订单信息统计", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(ErpOrderInfoStatisticsBo bo, HttpServletResponse response) {
//        List<ErpOrderInfoStatisticsVo> list = iErpOrderInfoStatisticsService.queryList(bo);
//        ExcelUtil.exportExcel(list, "订单信息统计", ErpOrderInfoStatisticsVo.class, response);
//    }
//
//    /**
//     * 获取订单信息统计详细信息
//     *
//     * @param id 主键
//     */
//    @SaCheckPermission("system:orderInfoStatistics:query")
//    @GetMapping("/{id}")
//    public R<ErpOrderInfoStatisticsVo> getInfo(@NotNull(message = "主键不能为空")
//                                     @PathVariable Long id) {
//        return R.ok(iErpOrderInfoStatisticsService.queryById(id));
//    }
//
//    /**
//     * 新增订单信息统计
//     */
//    @SaCheckPermission("system:orderInfoStatistics:add")
//    @Log(title = "订单信息统计", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PostMapping()
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpOrderInfoStatisticsBo bo) {
//        return toAjax(iErpOrderInfoStatisticsService.insertByBo(bo));
//    }
//
//    /**
//     * 修改订单信息统计
//     */
//    @SaCheckPermission("system:orderInfoStatistics:edit")
//    @Log(title = "订单信息统计", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
//    @PutMapping()
//    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpOrderInfoStatisticsBo bo) {
//        return toAjax(iErpOrderInfoStatisticsService.updateByBo(bo));
//    }
//
//    /**
//     * 删除订单信息统计
//     *
//     * @param ids 主键串
//     */
//    @SaCheckPermission("system:orderInfoStatistics:remove")
//    @Log(title = "订单信息统计", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public R<Void> remove(@NotEmpty(message = "主键不能为空")
//                          @PathVariable Long[] ids) {
//        return toAjax(iErpOrderInfoStatisticsService.deleteWithValidByIds(Arrays.asList(ids), true));
//    }
}
